package exilim.exf1.cspacker;

import java.io.File;
import java.util.Map;

import exilim.exf1.cspacker.worker.PackageSequencesWorker;
import exilim.exf1.cspacker.worker.ScanFolderWorker;

/**
 * 
 * @author Janos
 * @version 16.08.2010 | 02:44:28
 * 
 */
public class CSPackerRunner {

	public static void main(final String[] args) {

		if (args == null || args.length <= 0) {
			CSPackerGui.main(null);
			return;
		}

		final File sourceDirectory = new File(args[0]);

		if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) {
			System.err.println(String.format("Directory '%s' doesn't exist.", sourceDirectory.getPath()));
			System.exit(0);
			return;
		}

		System.out.println("line 34");
		System.out.println(sourceDirectory);
		final File[] jpgFiles = CSPackerLogic.getFileList(sourceDirectory);

		System.out.println(jpgFiles);

		System.out.println("Analyzing " + jpgFiles.length + " jpg image files in directory.");

		final ScanFolderWorker scanFolderWorker = new ScanFolderWorker(jpgFiles);
		scanFolderWorker.execute();
		Map<Integer, Map<Integer, File>> sequences;
		try {
			sequences = scanFolderWorker.get();
		} catch (final Exception e) {
			e.printStackTrace();
			System.exit(1);
			return;
		}
		System.out.println(String.format("Found %d sequences.", sequences.size()));

		final PackageSequencesWorker packageSequencesWorker = new PackageSequencesWorker(sourceDirectory, sequences);
		packageSequencesWorker.execute();
		try {
			packageSequencesWorker.get();
		} catch (final Exception e) {
			e.printStackTrace();
		}

		System.out.println("Finished.");

	}

}
